<?php

namespace app\tour\controller;
use think\Controller;
use think\View;
use think\Db;


/**
 * 自动执行脚本
 *
 * @author Administrator
 */
class Document extends Controller
{
    protected $_action;
    protected $_controller;
    public function _initialize() {
        $request = request();
        $this->_action = $request->action();
        if (session('document_login') != 1 && $this->_action != 'login') {
            //dump(session(''));exit;
            $this->redirect(url('login'));
        }
        if ($this->_action != 'index' && $this->_action != 'login' && session('document_admin') != 1 ) {
            $this->error('你没有权限', url('login'));
        }
    }

    public function index()
    {
        // 获取分页显示
        $where = [];
        $cid = input('param.cid', 0);
        $this->assign('cid', $cid);
        if ($cid > 0) {
            $where = ['cid' => $cid];
        }
        $order = 'id desc';

        if (request()->isAjax()) {

            $data = db('api')->where($where)->order($order)->paginate();
            $view = new View();
            $view->assign('data', $data);
            $tpl = $view->fetch('document/index_tpl');
            return json(['code' => 200, 'tpl' => $tpl]);
        } else {
            // 获取全部分类
            $category = get_group_cates('api_category');
            //dump($category);exit;
            $this->assign('category', $category);

            $data = db('api')->where($where)->order($order)->paginate();
            $data = $data->toArray();
            //dump($data); exit
            $this->assign('data', $data);

            $this->assign('cid', $cid);
            return view();
        }
    }

    public function save_api()
    {
        $id = input('param.id');
        if (request()->isAjax()) {
            $data = [
                'name' => input('param.name'),
                'api' => input('param.api'),
                'cid' => input('param.cid')
            ];
            // 启动事务
            Db::startTrans();
            try{
                if ($id > 0) {
                    Db::table('tour_api')->where('id', $id)->update($data);
                } else {
                    $id = Db::table('tour_api')->insertGetId($data);
                }
                // 提交事务
                Db::commit();
            } catch (\Exception $e) {
                // 回滚事务
                Db::rollback();
                return json(['code' => 0, 'msg' => $e->getMessage()]);
            }
            return json(['code' => 200, 'msg' => '保存成功！', 'id' => $id]);
        } else {
            $data = [];
            if ($id > 0) {
                $data = db('api')->where('id', $id)->find();
            }
            $this->assign('data', $data);
            $this->assign('id', $id);
            // 获取全部分类
            $category = get_group_cates('api_category');
            //dump($category);exit;
            $this->assign('category', $category);
            return view();
        }
    }


    public function api_list()
    {
        // 获取分页显示
        $where = [];
        $cid = input('param.cid', 0);
        $this->assign('cid', $cid);
        if ($cid > 0) {
            $where = ['cid' => $cid];
        }
        $order = 'id desc';

        if (request()->isAjax()) {

            $data = db('api')->where($where)->order($order)->paginate();
            $view = new View();
            $view->assign('data', $data);
            $tpl = $view->fetch('document/api_list_tpl');
            return json(['code' => 200, 'tpl' => $tpl]);
        } else {
            $this->assign('action', 'api');
            $this->assign('cid', $cid);
            // 获取全部分类
            $category = get_group_cates('api_category');
            //dump($category);exit;
            $this->assign('category', $category);

            $data = db('api')->where($where)->order($order)->paginate();
            $data = $data->toArray();
            //dump($data); exit;
            $this->assign('data', $data);
            return view();
        }
    }

    public function save_api_param()
    {
        $id = input('param.id');
        if (request()->isAjax()) {
            $data = [
                'key' => input('param.key'),
                'type' => input('param.type'),
                'example' => input('param.example'),
                'msg' => input('param.msg'),
                'api_id' => input('param.api_id')
            ];
            // 启动事务
            Db::startTrans();
            try{
                if ($id > 0) {
                    Db::table('tour_api_param')->where('id', $id)->update($data);
                } else {
                    $id = Db::table('tour_api_param')->insertGetId($data);
                }
                // 提交事务
                Db::commit();
            } catch (\Exception $e) {
                // 回滚事务
                Db::rollback();
                return json(['code' => 0, 'msg' => $e->getMessage()]);
            }
            return json(['code' => 200, 'msg' => '保存成功！', 'id' => $id]);
        } else {
            $type = input('param.type', '');
            $this->assign('type', $type);
            $data = db('api_param')->where('api_id', $id)->select();
            $this->assign('data', $data);
            $this->assign('id', $id);
            return view();
        }
    }

    public function save_api_returns()
    {
        $id = input('param.id');
        if (request()->isAjax()) {
            $data = [
                'key' => input('param.key'),
                'type' => input('param.type'),
                'example' => input('param.example'),
                'msg' => input('param.msg'),
                'api_id' => input('param.api_id')
            ];
            // 启动事务
            Db::startTrans();
            try{
                if ($id > 0) {
                    Db::table('tour_api_returns')->where('id', $id)->update($data);
                } else {
                    $id = Db::table('tour_api_returns')->insertGetId($data);
                }
                // 提交事务
                Db::commit();
            } catch (\Exception $e) {
                // 回滚事务
                Db::rollback();
                return json(['code' => 0, 'msg' => $e->getMessage()]);
            }
            return json(['code' => 200, 'msg' => '保存成功！', 'id' => $id]);
        } else {
            $type = input('param.type', '');
            $this->assign('type', $type);
            $data = db('api_returns')->where('api_id', $id)->select();
            $this->assign('data', $data);
            $this->assign('id', $id);
            return view();
        }
    }

    public function api_explain()
    {
        $id = input('param.id');
        if (request()->isAjax()) {
            $data = [
                'api_msg' => input('param.api_msg'),
            ];
            // 启动事务
            Db::startTrans();
            try{
                if ($id > 0) {
                    Db::table('tour_api')->where('id', $id)->update($data);
                } else {
                    $id = Db::table('tour_api')->insertGetId($data);
                }
                // 提交事务
                Db::commit();
            } catch (\Exception $e) {
                // 回滚事务
                Db::rollback();
                return json(['code' => 0, 'msg' => $e->getMessage()]);
            }
            return json(['code' => 200, 'msg' => '保存成功！', 'id' => $id]);
        } else {
            $type = input('param.type', '');
            $this->assign('type', $type);
            $data = [];
            if ($id > 0) {
                $data = db('api')->where('id', $id)->find();
            }
            $this->assign('data', $data);
            return view();
        }
    }

    // 删除api 、 参数 、 返回值
    public function delete_data() {
        if (request()->isAjax()){
            $id = input('param.id');
            $model = input('param.model');
            // 启动事务
            Db::startTrans();
            try{
                Db::table('tour_' . $model)->where('id', $id)->delete();
                // 提交事务
                Db::commit();
            } catch (\Exception $e) {
                // 回滚事务
                Db::rollback();
                return json(['code' => 0, 'msg' => $e->getMessage()]);
            }
            return json(['code' => 200, 'msg' => '删除成功！', 'id' => $id]);
        } else {
            return false;
        }
    }



    public function category() {
        $this->assign('action', 'category');
        // 获取全部分类
        $category = get_cates('api_category', 0);
        $this->assign('category', $category);
        return view();
    }

    public function save_category() {
        $id = input('param.id');
        if (request()->isAjax()) {
            //return json(input('param.'));
            try {
                $data = [
                    'name' => input('param.name', ''),
                    'pid' => input('param.pid', 0)
                ];
                if ($id > 0) {
                    db('api_category')->where('id', $id)->update($data);
                } else {
                    db('api_category')->insert($data);
                }
                return json(['code' => 200, 'msg' => 'success']);
            } catch (\Exception $e) {
                return json(['code' => 0, 'msg' => $e->getMessage()]);
            }
        } else {
            $data = [];
            if ($id > 0) {
                $data = db('api_category')->where('id', $id)->find();
            }
            $this->assign('data', $data);

            // 获取全部分类
            $category = $cate = get_cates('api_category', 0);
            //dump($category);
            $this->assign('category', $category);
            return view();
        }
    }

    public function login() {
        if (request()->isAjax()) {
            $username_array = ['admin', 'api'];
            $password_array = ['admin' => 'admin123', 'api' => 'api'];

            $username = input('param.username');
            $password = input('param.password');
            if (in_array($username, $username_array)) {
                if ($password == $password_array[$username]) {
                    session('document_login', 1);
                    if ($username == 'admin') {
                        session('document_admin', 1);
                    }
                    return json(['code' => 200, 'msg' => '登录成功!!']);
                } else {
                    return json(['code' => 0, 'msg' => '密码错误!!']);
                }
            } else {
                return json(['code' => 0, 'msg' => '帐号不存在!!']);
            }
        }
        return view();
    }

}
